package com.roc.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.roc.dao.IPagenitionDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.pojo.TblMgmInsCashHeadRoute;
import com.roc.service.CashHeadRouteService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
@Service
public class CashHeadRouteServiceImpl implements CashHeadRouteService{
	
	private Logger logger=LogManager.getLogger(CashHeadRouteServiceImpl.class);
	@Autowired
	private IPagenitionDao pageDao;
	
	@Autowired
	private ISQLBaseDao sqlBaseDao;
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<TblMgmInsCashHeadRoute> queryHeadRouteList(
			TblMgmInsCashHeadRoute tblMgmInsCashHeadRoute)
			throws ServiceException {
//		String sql ="select * from apstfr.TBL_mgm_ins_cash_head_route t where t.ins_cd_pre =?  and pro_cd = ? and route_ins_cd = ?";
		List<TblMgmInsCashHeadRoute> headList = new ArrayList<TblMgmInsCashHeadRoute>();
		List<Object> paramsList =  new ArrayList<Object>();
		try {
			
			String selectSql = "";
			StringBuffer sb = new StringBuffer();
			sb.append("select t.INS_CD_PRE,t.PROC_CD,t.ROUTE_INS_CD,t.DEFAULT_HEAD,t.AVALIABLE_HEAD,t.RESVER1,t.RESVER2,t.RESVER3,t.RESVER4,(case when t.ROUTE_INS_CD='1' then '银联多渠道代付' when t.ROUTE_INS_CD='2' then '银嘉代付' else '未知通道' end) routeName,(case when t.PROC_CD='1171' then 'D0代付' when t.PROC_CD='2100' then '单笔实时代付' end) procName,a.ins_name from apstfr.TBL_mgm_ins_cash_head_route t left join tbl_ins_inf a on t.INS_CD_PRE=a.ins_id_cd where 1=1 ");
			if(tblMgmInsCashHeadRoute != null && tblMgmInsCashHeadRoute.getInsCdPre() != null && !"".equals(tblMgmInsCashHeadRoute.getInsCdPre())){
				sb.append(" and t.ins_cd_pre =?");
				paramsList.add(Constance.Null2EmptyTrim(tblMgmInsCashHeadRoute.getInsCdPre()));//
			}
			if(tblMgmInsCashHeadRoute != null && tblMgmInsCashHeadRoute.getProcCd() !=null && !"".equals(tblMgmInsCashHeadRoute.getProcCd())){
				sb.append("and proc_cd = ?");
				paramsList.add(Constance.Null2EmptyTrim(tblMgmInsCashHeadRoute.getProcCd()));//
			}
			if(tblMgmInsCashHeadRoute != null && tblMgmInsCashHeadRoute.getRouteInsCd() != null && !"".equals(tblMgmInsCashHeadRoute.getRouteInsCd())){
				if("0".equals(Constance.Null2EmptyTrim(tblMgmInsCashHeadRoute.getRouteInsCd()))){
					sb.append("and route_ins_cd not in ('1','2')");
				}else{
					sb.append("and route_ins_cd = ?");
					paramsList.add(Constance.Null2EmptyTrim(tblMgmInsCashHeadRoute.getRouteInsCd()));//
				}
				
			}
			selectSql = sb.toString();
			List<Object> resList = pageDao.findAllByParams(selectSql, paramsList, 0, 20);
			
			dealRsListToHeadList(headList,resList);
		} catch (Exception e) {
			logger.error("头寸信息查询异常:  "+e);
			throw new ServiceException("头寸信息查询异常");
		}
		
		return headList;
	}

	private void dealRsListToHeadList(List<TblMgmInsCashHeadRoute> headList,
			List<Object> resList) {
		DecimalFormat df = new DecimalFormat("0.00");
		if(resList != null && resList.size()>0){
			Iterator it = resList.iterator();
			while(it.hasNext()){
				Object[] objArray = (Object[])it.next();
				TblMgmInsCashHeadRoute tblMgmInsCashHeadRoute = new TblMgmInsCashHeadRoute();
				tblMgmInsCashHeadRoute.setInsCdPre(Constance.Null2EmptyTrim(objArray[0]));
				tblMgmInsCashHeadRoute.setProcCd(Constance.Null2EmptyTrim(objArray[1]));
				tblMgmInsCashHeadRoute.setRouteInsCd(Constance.Null2EmptyTrim(objArray[2]));
//				tblMgmInsCashHeadRoute.setDefaultHead(Constance.Null2EmptyTrim(objArray[3]));
//				tblMgmInsCashHeadRoute.setAvaliableHead(Constance.Null2EmptyTrim(objArray[4]));
//				tblMgmInsCashHeadRoute.setResver1(Constance.Null2EmptyTrim(objArray[5]));
//				tblMgmInsCashHeadRoute.setResver2(Constance.Null2EmptyTrim(objArray[6]));
				String defaultHead = Constance.Null2EmptyTrim(objArray[3]);
				if("".equals(defaultHead)){
					tblMgmInsCashHeadRoute.setDefaultHead("0.00");
				}else{
					tblMgmInsCashHeadRoute.setDefaultHead(df.format(new BigDecimal(defaultHead).divide(new BigDecimal(100))));
				}
				String avaliableHead = Constance.Null2EmptyTrim(objArray[4]);
				if("".equals(avaliableHead)){
					tblMgmInsCashHeadRoute.setAvaliableHead("0.00");
				}else{
					tblMgmInsCashHeadRoute.setAvaliableHead(df.format(new BigDecimal(avaliableHead).divide(new BigDecimal(100))));
				}
				String resver1 = Constance.Null2EmptyTrim(objArray[5]);
				if("".equals(resver1)){
					tblMgmInsCashHeadRoute.setResver1("0.00");
				}else{
					tblMgmInsCashHeadRoute.setResver1(String.valueOf(new BigDecimal(resver1).divide(new BigDecimal(100)).doubleValue()));
				}
				String resver2 = Constance.Null2EmptyTrim(objArray[6]);
				if("".equals(resver2)){
					tblMgmInsCashHeadRoute.setResver2("0.00");
				}else{
					tblMgmInsCashHeadRoute.setResver2(String.valueOf(new BigDecimal(resver2).divide(new BigDecimal(100)).doubleValue()));
				}
				tblMgmInsCashHeadRoute.setResver3(Constance.Null2EmptyTrim(objArray[7]));
				tblMgmInsCashHeadRoute.setResver4(Constance.Null2EmptyTrim(objArray[8]));
				tblMgmInsCashHeadRoute.setRouteName(Constance.Null2EmptyTrim(objArray[9]));
				tblMgmInsCashHeadRoute.setProcName(Constance.Null2EmptyTrim(objArray[10]));
				tblMgmInsCashHeadRoute.setInsName(Constance.Null2EmptyTrim(objArray[11]));
				headList.add(tblMgmInsCashHeadRoute);
			}
		}
		
	}

	@Transactional(rollbackFor = { ServiceException.class })
	@Override
	public void modifySomeHeadRoute(
			TblMgmInsCashHeadRoute t)
			throws ServiceException {
		String updateSql = "update apstfr.TBL_mgm_ins_cash_head_route t set t.default_head = ?,t.avaliable_head=?,t.resver1=?,t.resver2=?,t.resver3=?,t.resver4=? where t.ins_cd_pre=? and t.proc_cd=? and t.route_ins_cd=?";
		try {
			List<Object> params = new ArrayList<Object>();
			if(t != null){
				//String.valueOf(new BigDecimal().multiply(new BigDecimal(100)).longValue())
				params.add(String.valueOf(new BigDecimal(Constance.Null2EmptyTrim(t.getDefaultHead())).multiply(new BigDecimal(100)).longValue()));//Constance.Null2EmptyTrim(t.getDefaultHead())
				String avaliableHead = String.valueOf(new BigDecimal(Constance.Null2EmptyTrim(t.getAvaliableHead())).multiply(new BigDecimal(100)).longValue());//Constance.Null2EmptyTrim(t.getAvaliableHead())
				String avaliableHeadAdd = String.valueOf(new BigDecimal(Constance.Null2EmptyTrim(t.getAvaliableHeadAdd())).multiply(new BigDecimal(100)).longValue());//Constance.Null2EmptyTrim(t.getAvaliableHeadAdd())
				BigDecimal totalHead = new BigDecimal(avaliableHead).add(new BigDecimal(avaliableHeadAdd));
				params.add(totalHead.toString());
				params.add(String.valueOf(new BigDecimal(Constance.Null2EmptyTrim(t.getResver1())).multiply(new BigDecimal(100)).longValue()));//Constance.Null2EmptyTrim(t.getResver1())
				params.add(String.valueOf(new BigDecimal(Constance.Null2EmptyTrim(t.getResver2())).multiply(new BigDecimal(100)).longValue()));//Constance.Null2EmptyTrim(t.getResver2())
				params.add(Constance.Null2EmptyTrim(t.getResver3()));
				params.add(Constance.Null2EmptyTrim(t.getResver4()));
				params.add(t.getInsCdPre());
				params.add(t.getProcCd());
				params.add(t.getRouteInsCd());
			}
			sqlBaseDao.deleteBySql(updateSql, params);
		} catch (Exception e) {
			logger.error("头寸信息更新异常："+e);
			throw new ServiceException();
		}
	}
}
